//往集合中添加单个文档

const MongoClient = require('mongodb').MongoClient;
const url = 'mongodb://localhost:27017';
// const dbName = 'runoob';
const dbName = 'testmgdb';
//连接到MongoDB
MongoClient.connect(url, { useNewUrlParser: true }, function(err, client) {
    if (err) throw err;
    const db = client.db(dbName); //选择数据库
    const coll = db.collection('bookinfo'); // 选择一个集合(表)
    //执行添加文档操作


    // 添加多个文档
    const myobj = [{
        "isbn": "19301042",
        "name": "Spring MVC",
        "author": "小小小 ",
        "press ": "人民邮电出版社 ",
        "price ": "89.00 ",
        "pubdate ": "2016 - 07 - 01 "
    }, {
        "isbn": "19301043",
        "name": "数学之美（第二版）",
        "author": "吴军",
        "press": "人民邮电出版社 ",
        "price ": "49.00 ",
        "pubdate ": "2014 - 11 - 01 "
    }];
    db.collection('bookinfo').insertMany(myobj, function(err, res) {
        if (err) throw err;
        console.log("插入的文档数为: " + res.insertedCount);
    });


});